package com.camerareader.mvp.image_list_page.extras;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.camerareader.R;

import java.io.File;

/**
 * Created by Administrator on 2016-12-06.
 */

public class ImageItemView extends FrameLayout {
    private ImageView image;
    private ImageView selectedState;
    public ImageItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public ImageItemView(Context context) {
        this(context, null);
    }

    public ImageItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private void initView(){
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.setMargins(5, 5, 5, 5);
        setLayoutParams(params);
        View.inflate(getContext(), R.layout.thumbnai_item_view, this);
        image = (ImageView) findViewById(R.id.thumbnail_image);
        selectedState = (ImageView) findViewById(R.id.selected_state);
    }


    public void setStateSelected(boolean selected){
        if (selected){
            image.setColorFilter(ContextCompat.getColor(getContext(), R.color.image_mask_color));
            selectedState.setVisibility(VISIBLE);
        }else {
            image.setColorFilter(ContextCompat.getColor(getContext(), R.color.transparent));
            selectedState.setVisibility(GONE);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(widthMeasureSpec);
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }

    public void setImageResource(String path, String fileName){
        Glide.with(getContext()).load(new File(path + fileName))
                .placeholder(ContextCompat.getDrawable(getContext(), R.drawable.image_placeholder))
                .centerCrop()
                .into(image);
    }

}
